Ext.define('Seq.svg.Keys', {
	        extend : 'Seq.svg.Base',

	        buffer : null,
	        isCopy : false,
	        isCut : false,
	        pastes : 0,

	        constructor : function(canvas) {
		        var me = this;
		        me.callParent(arguments);

		        map = new Ext.KeyMap(document);
		        map.addBinding({
			                key : [Ext.event.Event.UP, Ext.event.Event.DOWN, Ext.event.Event.RIGHT,
			                        Ext.event.Event.LEFT],
			                shift : true,
			                fn : me.move,
			                scope : me
		                });
		        map.addBinding({
			                key : [Ext.event.Event.DELETE],
			                fn : me.remove,
			                scope : me
		                });
		        map.addBinding({
			                key : [Ext.event.Event.C, Ext.event.Event.V],
			                ctrl : true,
			                fn : me.cv,
			                scope : me
		                });
		        map.addBinding({
			                key : [Ext.event.Event.ESC],
			                fn : me.esc,
			                scope : me
		                });
		        map.addBinding({
			                key : [Ext.event.Event.Z],
			                ctrl : true,
			                fn : me.undo,
			                scope : me
		                });
	        },

	        transformItems : function(items, matrix) {
		        for (var i = 0, len = items.length; i < len; i++) {
			        var item = items[i], m = item.transform().localMatrix.add(matrix);
			        item.transform(m);
		        }
	        },

	        transformSelected : function(matrix) {
		        var me = this, selectBox = me.canvas.selectBox;

		        me.transformItems(selectBox.selected, matrix);
		        me.transformItems(selectBox.subBoxs, matrix);
		        selectBox.box.transform(selectBox.box.transform().localMatrix.add(matrix));
		        me.transformItems(selectBox.anchors, matrix);
	        },

	        move : function(key, e) {
		        if ('text' == e.target.type) {
			        return;
		        }
		        var me = this, m;
		        if (e.UP === key) {
			        m = new Snap.Matrix(1, 0, 0, 1, 0, -1);
		        } else if (e.DOWN === key) {
			        m = new Snap.Matrix(1, 0, 0, 1, 0, 1);
		        } else if (e.LEFT === key) {
			        m = new Snap.Matrix(1, 0, 0, 1, -1, 0);
		        } else if (e.RIGHT === key) {
			        m = new Snap.Matrix(1, 0, 0, 1, 1, 0);
		        }
		        if (m) {
			        me.transformSelected(m);
		        }
	        },

	        remove : function() {
		        if ('text' == e.target.type) {
			        return;
		        }
		        var selectBox = me.canvas.selectBox, selected = selectBox.selected;
		        for (var i = 0, len = selected.length; i < len; i++) {
			        selected[i].remove();
		        }
		        selectBox.clear();
	        },

	        cv : function(key, e) {
		        if ('text' == e.target.type) {
			        return;
		        }
		        var me = this;
		        if (Ext.event.Event.C === key) {
			        me.copy(e);
		        } else if (Ext.event.Event.V === key) {
			        me.paste(e);
		        }
	        },

	        copy : function(e) {
		        var me = this, selected = me.canvas.selectBox.selected;
		        me.buffer = selected;
		        me.pastes = 1;
	        },

	        paste : function(e) {
		        var me = this;
		        if (me.buffer) {
			        var selected = [], matrix = new Snap.Matrix(1, 0, 0, 1, me.pastes * 10, me.pastes * 10);
			        for (var i = 0, len = me.buffer.length; i < len; i++) {
				        var item = me.buffer[i].clone(), m = item.transform().localMatrix.add(matrix);
				        item.transform(m);
				        selected.push(item);
				        // g节点，需要加入到models中，点击才能选中
				        if ('g' === item.type) {
					        me.canvas.models.push(item);
				        }
			        }
			        me.pastes++;
			        // 选中新MODEL
			        me.canvas.selectBox.clear();
			        me.canvas.selectBox.selects(selected, e);
		        }
	        },

	        undo : function(key, e) {
		        if ('text' == e.target.type) {
			        return;
		        }
	        },

	        esc : function(key, e) {
		        if ('text' == e.target.type) {
			        return;
		        }
		        var me = this, canvas = me.canvas;
		        canvas.selectBox.cancel();
		        canvas.drager.cancel();
		        canvas.selector.cancel();
		        canvas.resizer.cancel();
	        }

        });